package com.ymatou.iapi.optrade.resp;

import java.math.BigDecimal;
import java.util.List;

import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFeeTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.AuthenticateFreightTypeEnum;
import com.ymttest.common.order.userquery.util.Utils;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;

public class Order {

	private OrderPo orderPo;

	private List<OrderDetailPo> orderDetailPoList;

	public OrderPo getOrderPo() {
		return orderPo;
	}

	public void setOrderPo(OrderPo orderPo) {
		this.orderPo = orderPo;
	}

	public long getOrderId() {
		return orderPo.getOrderId();
	}

	public Long getSellerId() {
		return orderPo.getSellerId();
	}

	/**
	 * 订单总运费
	 * @return
	 */
	public BigDecimal getOrderFreight() {
		return orderPo.getFreight();
	}

	/**
	 * 订单应付金额
	 * @return
	 */
	public BigDecimal getOrderPayableAmount() {
		return orderPo.getPayableAmount();
	}
	public List<OrderDetailPo> getOrderDetailPoList() {
		return orderDetailPoList;
	}

	public void setOrderDetailPoList(List<OrderDetailPo> orderDetailPoList) {
		this.orderDetailPoList = orderDetailPoList;
	}

	public BigDecimal getUserAuthenticateFee(Integer productCount) {

		return orderDetailPoList
				.stream()
				.filter(orderDetailPo -> Utils.zeroIfNull(orderDetailPo
						.getAuthenticateFeeType()) == AuthenticateFeeTypeEnum.BUYER
						.getCode())
				.map(orderDetailPo -> Utils.zeroIfNull(
						orderDetailPo.getAuthenticateFee()).multiply(
						new BigDecimal(productCount)))
				.reduce(BigDecimal.ZERO, BigDecimal::add);
	}
	
	public BigDecimal getUserAuthenticateFee(Integer productCount,
			String catalogId) {

		return orderDetailPoList
				.stream()
				.filter(orderDetailPo -> Utils.zeroIfNull(orderDetailPo
						.getAuthenticateFeeType()) == AuthenticateFeeTypeEnum.BUYER
						.getCode()
						&& orderDetailPo.getCatalogId().equalsIgnoreCase(
								catalogId))
				.map(orderDetailPo -> Utils.zeroIfNull(
						orderDetailPo.getAuthenticateFee()).multiply(
						new BigDecimal(productCount)))
				.reduce(BigDecimal.ZERO, BigDecimal::add);
	}
	
	public BigDecimal getUserAuthenticateFreightFee() {
		
		return Utils.zeroIfNull(orderPo.getAuthenticateFreightType()) == AuthenticateFreightTypeEnum.BUYER
				.getCode() ? Utils.zeroIfNull(orderPo.getAuthenticateFreight())
				: BigDecimal.ZERO;
	}
}
